<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/18
 * Time: 14:49
 */

namespace app\api\controller\v1;
use think\facade\Request;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use Qiniu\Config;
use think\Db;
class Qny extends Base
{
    public function upload()
    {

        if (request()->isPost()) {
            $files = request()->file('images');

            $config = config('qny');
            $auth = new Auth($config['AccessKey'], $config['SecretKey']);

            $token = $auth->uploadToken($config['BUCKET']);

            // 构建 UploadManager 对象
            $uploadMrg = new UploadManager();

            // 上传文件到七牛
            $files = $_FILES;
            $values = array_values($files);
            $domain = $config['DOMAIN'];
            $saveName = hash_file('sha1', $values[0]['tmp_name']) . time();
            list($ret, $err) = $uploadMrg->putFile($token, $saveName, $values[0]['tmp_name']);

            if ($err !== null) {

                return_json(200, '上传失败');
            } else {


                $img = 'http://' . $domain . '/' . $ret['key'];
                $arr = array();
                $arr['images'] = $img;

                return_json(200, '上传成功', $img);
            }

        }

    }


   public function delete(){

        $uid = Request::param('id');
        $token = request()->header('token');
        $this->verifyToken($uid,$token);
        $img = Request::param('club');
   
        $delImgName = $img;


            // 初始化签权对象
        $configs = config('qny');
        $auth = new Auth($configs['AccessKey'],$configs['SecretKey']);


        $config = new Config();

//        管理资源
        $bucketManager = new BucketManager($auth,$config);

//        删除文件操作

        $res = $bucketManager -> delete($configs['BUCKET'],$delImgName);

        if (is_null($res)) {
           
            
            return_json(200,'删除成功！',true);
          
        }else{
       
            return_json(400,'删除失败！',false);
        }

    
}

}